package com.saifan.wyy_ov.ui.communitylife;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;

import android.support.v7.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupMenu;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.FleaMarketInfoBean;
import com.saifan.wyy_ov.data.bean.PermissionsBean;
import com.saifan.wyy_ov.data.bean.RequestBean;
import com.saifan.wyy_ov.data.bean.RoomInfoBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.LabelView;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-11 20:25:09
 * @descrption 跳蚤市场
 */

public class FleamarketActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private ListView listView;
    private CheckBox checkbox;
    private RefreshLayout refreshLayout;
    private int page = 0;
    private RoomInfoBean roomInfoBean;
    private RequestBean requestBean;
    private GetData getData;
    private CommonAdapter<FleaMarketInfoBean> adapter;
    private List<FleaMarketInfoBean> beans = new ArrayList<>();
    private int LX = 0;
    private View loadlay;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomInfoBean = getRoomInfoBean();
        requestBean = new RequestBean();
        requestBean.setXMBS(roomInfoBean.getXMBS());
        getData = new GetData();
        setContentView();
        initAdapter();
        query();


    }

    private void initAdapter() {
        adapter = new CommonAdapter<FleaMarketInfoBean>(this, beans, R.layout.fleamarket_item) {
            @Override
            public void convert(ViewHolderHelper helper, FleaMarketInfoBean item) {

                helper.setText(R.id.TZSC_BT, StringUtil.returnShow(item.getTZSC_BT()));
                helper.setText(R.id.TZSC_FBSJ, StringUtil.returnShow(item.getTZSC_FBSJ()));
                helper.setText(R.id.TZSC_JE, StringUtil.returnShow(getString(R.string.rmb, PriceUtils.getPrice(item.getTZSC_JE()))));
                helper.setImageByUrl(R.id.TZSC_SLT, item.getTZSC_SLT());
                LabelView label = helper.getView(12345);
                if (label == null) {
                    label = new LabelView(FleamarketActivity.this);
                    label.setId(12345);

                }
                if (item.getTZSC_LX() == 3) {
                    label.setBackgroundColor(getResources().getColor(R.color.state3));
                    label.setTargetViewInBaseAdapter(helper.getView(R.id.TZSC_SLT), 92, 10, LabelView.Gravity.LEFT_TOP);
                    label.setText("赠送");
                } else if (item.getTZSC_LX() == 1) {
                    label.setBackgroundColor(getResources().getColor(R.color.state2));
                    label.setTargetViewInBaseAdapter(helper.getView(R.id.TZSC_SLT), 92, 10, LabelView.Gravity.LEFT_TOP);
                    label.setText("转让");
                } else if (item.getTZSC_LX() == 2) {
                    label.setBackgroundColor(getResources().getColor(R.color.state1));
                    label.setTargetViewInBaseAdapter(helper.getView(R.id.TZSC_SLT), 92, 10, LabelView.Gravity.LEFT_TOP);
                    label.setText("置换");
                }


            }
        };
        listView.setAdapter(adapter);
    }

    public void setContentView() {
        setContentView(R.layout.activity_fleamarket);
        loadlay = findViewById(R.id.loading_lay);
        this.refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        this.listView = (ListView) findViewById(R.id.listView);
        this.checkbox = (CheckBox) findViewById(R.id.checkbox);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        checkbox.setOnClickListener(this);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                if (page > 0) {
                    refreshLayout.setLoading(true);
                }
                page++;
                query();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(FleamarketActivity.this, FleaMarketItemActivity.class);
                intent.putExtra("data", beans.get(position));
                startActivity(intent);


            }
        });


    }

    public void query() {
        requestBean.setPage(page);
        if (beans.isEmpty()) {
            loadlay.setVisibility(View.VISIBLE);
        }
        requestBean.setTZSC_LX(LX);
        getData.getDataJson(this, Api.FleaMarket.FLEA_MARKET_INFO, requestBean, "", new ResponseData() {
            @Override
            public void onSuccess(String json) {
                List<FleaMarketInfoBean> list = new Gson().fromJson(json, new TypeToken<List<FleaMarketInfoBean>>() {
                }.getType());
                if (list != null && !list.isEmpty()) {
                    if (page < 1) {
                        beans = list;
                    } else {
                        beans.addAll(list);
                    }


                } else {
                    if (page > 0) {
                        ToastUtil.showToast(getBaseContext(), "信息已全部显示完毕！");
                        page--;
                    } else {
                        ToastUtil.showToast(getBaseContext(), "暂无更多信息");
                        beans.clear();
                    }


                }

                adapter.mDatas = beans;
                adapter.notifyDataSetChanged();
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);


            }

            @Override
            public void onFailed(String str) {
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);
                if (page > 0) page--;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_fleamarket, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            if (getRoomInfoBean().getUserType() == 2){
                for (PermissionsBean permissionsBean : getPermissions()){
                    if (permissionsBean.getYKQX_GNMC().equals("跳蚤市场")){
                        startActivity(FleamarkReleaseActivity.class);
                        return true;
                    }
                }
                ToastUtil.showToast(FleamarketActivity.this, "您的权限不能进行此操作");
                return true;
            } else if (getRoomInfoBean().getUserType() == 3){
                ToastUtil.showToast(FleamarketActivity.this, "您的权限不能进行此操作");
                return true;
            }
            startActivity(FleamarkReleaseActivity.class);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {


        PopupMenu popupMenu = new PopupMenu(this, v);
        popupMenu.getMenuInflater().inflate(R.menu.menu_fleamarket_type, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int i = item.getItemId();
                if (i == R.id.item0) {
                    LX = 0;
                    page = 0;
                    query();

                } else if (i == R.id.item1) {
                    LX = 1;
                    page = 0;
                    query();

                } else if (i == R.id.item2) {
                    LX = 2;
                    page = 0;
                    query();

                } else if (i == R.id.item3) {
                    LX = 3;
                    page = 0;
                    query();

                } else {
                }
                checkbox.setText(item.getTitle());
                return true;
            }
        });
        popupMenu.show();


    }
}
